﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Web;

namespace CMS.Commons
{
    public class XmlUltil
    {
        /// DeSerialize string to objec
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static T DeSerializeToObject<T>(string value)
        {
            try
            {
                XmlSerializer x = new XmlSerializer(typeof(T));
                StringReader reader = new StringReader(value);
                T returnValue = (T)x.Deserialize(reader);
                reader.Close(); reader.Dispose();

                return returnValue;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Serialize obj to string
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string SerializeToString<T>(T obj)
        {
            StringWriter sw = new StringWriter();
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            serializer.Serialize(sw, obj);

            return sw.ToString();
        }

        /// <summary>
        /// Save file to Xml from Obj
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="filePath"></param>
        public static void SaveFileToXml<T>(T obj, string filePath)
        {
            using (FileStream fileStream = new FileStream(filePath, FileMode.Create))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                serializer.Serialize(fileStream, obj);
            }
        }

        /// <summary>
        /// Load obj from file
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static T LoadObjFromXml<T>(string filePath)
        {
            using (FileStream fileStream = new FileStream(filePath, FileMode.Open))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(T));

                T value = (T)serializer.Deserialize(fileStream);
                return value;
            }
        }

        public static string ReadFile(string fileName)
        {
            StreamReader streamReader = null;
            string text = string.Empty;

            try
            {
                if (File.Exists(fileName) == false)
                {
                    throw new Exception("Cannot read the file '" + fileName + "' because it does not exist!");
                }
                text = System.IO.File.ReadAllText(fileName);
            }
            catch (Exception ex)
            {
                throw new Exception("ReadFile() failed with error: " + ex.Message);
            }

            finally
            {
                if (streamReader != null)
                    streamReader.Close();
            }
            return text;
        }
        
        /// <summary>
        /// Validate Stamp XML
        /// </summary>
        /// <param name="stampXML"></param>
        /// <param name="isValid"></param>
        /// <param name="stringData"></param>
        /// <returns></returns>
        public static string ValidateStampXML(string stampXML, ref bool isValid, StringBuilder stringData)
        {
            var booksSettings = new XmlReaderSettings();
            string xsd = HttpContext.Current.Server.MapPath("~/WebForms/stampXSD.xsd");
            booksSettings.Schemas.Add(null, xsd);
            booksSettings.ValidationType = ValidationType.Schema;
            var reader = XmlReader.Create(new StringReader(stampXML), booksSettings);
            try
            {
                while (reader.Read()) ;
            }
            catch (Exception error)
            {
                isValid = false;
                return stringData.Append("Incorrect format:" + error.Message).ToString();
            }
            finally
            {
                reader.Close();
            }
            return string.Empty;
        }
       
    }
}
